package com.example.sunyueproject;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import android.net.LocalServerSocket;
import android.net.LocalSocket;
import android.net.LocalSocketAddress;
import android.os.Environment;
import android.util.Log;

public class RTVideoUpload {
	
	private Socket s;
	private LocalSocket socket;
	private InputStream ll;
	private FileOutputStream oo;
	
	public void createConnection(String ip, int port) {
		File tmpfile = null;
		LocalServerSocket lss;
		try{
		tmpfile = new File(Environment.getExternalStorageDirectory().getCanonicalFile()+"/tmp3.mp4");
		lss = new LocalServerSocket("h264");
		LocalSocketAddress addr = new LocalSocketAddress("h264");
		oo = new FileOutputStream(tmpfile);
		socket = new LocalSocket();
		socket.connect(addr);
		ll = socket.getInputStream();
		//ll = lss.accept().getInputStream();
		
		new Thread(new Runnable() {

			public void run() {
				byte[] buf = new byte[1024*32];
				while(true) {
					int i=0;
					try {
						i = ll.read(buf);
					
					if(i==-1)
						break;
					oo.write(buf, 0, i);
					} catch (IOException e) {
						Log.d("networking",e.getMessage());
						break;
					}
				}
			}}
		
			).start();
		
		} catch(Exception e) {
			Log.d("networking", tmpfile.getAbsolutePath() + " " + e.getMessage());
		}
	}
	
	public FileDescriptor getFd() {
		return socket.getFileDescriptor();
	}
	
}
